package com.cyz.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cyz.dao.UserDao;
import com.cyz.pojo.User;
import com.cyz.util.DBOperation;
import com.cyz.util.Dbutil;
import com.cyz.util.MD5Util;

public class UserDaoImpl implements UserDao{

	static Connection Conn=Dbutil.getConnection();
	static PreparedStatement pst;
	

	/**
	 * ͨ���û�����ȡ�û�ID
	 */

	@Override
	public Integer getUserIdByUserName(String userName) {
		ResultSet rs = null;
		Integer id = null;
		try {
			//pst=Conn.prepareStatement("select * from user where userName=?");
			//rs = pst.executeQuery();
			String sql = "select id from user where userName=?";
			List<Object> param = new ArrayList<Object>();
			param.add(userName);
			rs = DBOperation.query(sql, param);
			while (rs.next()) 	 
			{
			    id = rs.getInt("id");

			  }
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	
        return id;

	}

	/**
	 * ��ȡ�û�ID������
	 */
	@Override
	public User getUserById(Integer id) {
			User arr = null;
			ResultSet rs = null;
			try {
				//pst=Conn.prepareStatement("select * from user where id =id");
				String sql = "select * from user where id =?";
				List<Object> param = new ArrayList<Object>();
				param.add(id);
				rs = DBOperation.query(sql, param);
				//rs = pst.executeQuery();
				
				while (rs.next()) 
				{
					arr=new User();
					arr.setId(rs.getInt("id"));
					arr.setUserName(rs.getString("userName"));
					arr.setPassword(rs.getString("password"));
					arr.setMobile(rs.getString("mobile"));
					arr.setAddress(rs.getString("address"));
					arr.setBalance(rs.getFloat("balance"));
					
				  }
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		
	        return  arr;
	}
/**
 * �����û�
 * @throws SQLException 
 */
	@Override
	public void register(String userName, String password, String phone, String address) throws SQLException {
		ResultSet rs = null;
	    String sql="insert into user(userName,password,mobile,address) values(?,?,?,?)";
	  	List<Object> param = new ArrayList<Object>();
		param.add(userName);
		param.add(MD5Util.toMd5(password));
		param.add(phone);
		param.add(address);
		
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		 

	}
		//////////////////////
	/**
	 * 
	 *通过名字来获取用户对象
	 * @param
	 * @return
	 */
	@Override
	public User getUserByName(String userName) {
		User user = null;
		ResultSet rs = null;
		try {
			String sql = "select * from user where userName =?";
			List<Object> param = new ArrayList<Object>();
			param.add(userName);
			rs = DBOperation.query(sql, param);
			
			while (rs.next()) 
			{
				user=new User();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setMobile(rs.getString("mobile"));
				user.setAddress(rs.getString("address"));
				user.setBalance(rs.getFloat("balance"));
				
			  }
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	
	    return  user;
	}
	/**
	 * 通过用户名字来获取用户ID
	 *
	 * @param
	 * @return
	 */
	@Override
	public Integer getUserIdByName(String userName) {
		Integer id=null;
		ResultSet rs = null;
		try {
			String sql = "select * from user where userName =?";
			List<Object> param = new ArrayList<Object>();
			param.add(userName);
			rs = DBOperation.query(sql, param);
			
			while (rs.next()) 
			{
				id=rs.getInt("id");
				
				
			  }
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	
	    return  id;
	
	}
	/**
	 * 添加新用户
	 *
	 * @param
	 * @return
	 */
	@Override
	public void addUser(User user) {
			ResultSet rs = null;
		    String sql="insert into user(userName,password,mobile,address,balance) values(?,?,?,?,?)";
		  	List<Object> param = new ArrayList<Object>();
			param.add(user.getUserName());
			param.add(MD5Util.toMd5(user.getPassword()));
			param.add(user.getMobile());
			param.add(user.getAddress());
			param.add(user.getBalance());
			try {
				DBOperation.update(sql, param);
			} catch (SQLException e) {
				e.printStackTrace();
			}
	
	
	
	}
	/**
	 * 更新新用户
	 */
	@Override
	public void updateUser(User user) {
		ResultSet rs = null;
	    String sql="update user set userName=?,password=?,mobile=?,address=?,balance=? where id=?";
	  	List<Object> param = new ArrayList<Object>();
		param.add(user.getUserName());
		param.add(MD5Util.toMd5(user.getPassword()));
		param.add(user.getMobile());
		param.add(user.getAddress());
		param.add(user.getBalance());
		param.add(user.getId());
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过ID来查找当前用户
	 */
	@Override
	public User findUserById(Integer id) {
		User user = null;
		ResultSet rs = null;
		try {
			String sql = "select * from user where id =?";
			List<Object> param = new ArrayList<Object>();
			param.add(id);
			rs = DBOperation.query(sql, param);
			
			while (rs.next()) 
			{
				user=new User();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setMobile(rs.getString("mobile"));
				user.setAddress(rs.getString("address"));
				user.setBalance(rs.getFloat("balance"));
				
			  }
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	
	    return  user;

	}
	/**
	 * 通过名字来查找用户
	 */
	@Override
	public User findUserByUserName(String userName) {
		User user = null;
		ResultSet rs = null;
		try {
			String sql = "select * from user where userName =?";
			List<Object> param = new ArrayList<Object>();
			param.add(userName);
			rs = DBOperation.query(sql, param);
			
			while (rs.next()) 
			{
				user=new User();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setMobile(rs.getString("mobile"));
				user.setAddress(rs.getString("address"));
				user.setBalance(rs.getFloat("balance"));
				
			  }
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	
	    return  user;

	}
	/**
	 * 获取所有用户
	 */
	@Override
	public List<User> getAllUser() {
		String sql="select * from user";
		ResultSet rs=null;
		List<User> userList=new ArrayList();
		List<Object> param = new ArrayList<Object>();
		rs=DBOperation.query(sql,param);
		try {
			while(rs.next())
			{
				User user=new User();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setMobile(rs.getString("mobile"));
				user.setAddress(rs.getString("address"));
				user.setBalance(rs.getFloat("balance"));
				userList.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return userList;
	}
	/**
	 * 插入新用户
	 */
	@Override
	public Integer insertUser(User user) {
		ResultSet rs = null;
	    String sql="insert into user(userName,password,mobile,address,balance) values(?,?,?,?,?)";
	  	List<Object> param = new ArrayList<Object>();
		param.add(user.getUserName());
		param.add(MD5Util.toMd5(user.getPassword()));
		param.add(user.getMobile());
		param.add(user.getAddress());
		param.add(user.getBalance());
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	@Override
	public Integer deleteUser(User user) {
		
		String sql = "delete from user where id =?";
		List<Object> param = new ArrayList<Object>();
		param.add(user.getId());
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Integer getIdByUserName(String userName) {
		User user = null;
		ResultSet rs = null;
		Integer id=null;
		try {
			String sql = "select * from user where userName =?";
			List<Object> param = new ArrayList<Object>();
			param.add(userName);
			rs = DBOperation.query(sql, param);
			
			while (rs.next()) 
			{
			
				id=rs.getInt("id");
		
			  }
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	
	    return  id;
	}
	   
	@Override
	public String getPasswordByUserName(String name) {
		User user = null;
		ResultSet rs = null;
		String password=null;
		try {
			String sql = "select * from user where userName =?";
			List<Object> param = new ArrayList<Object>();
			param.add(name);
			rs = DBOperation.query(sql, param);
			
			while (rs.next()) 
			{
				password=rs.getString("password");
		
		
			  }
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	
	    return  password;
	}

	public void updateInfo(Integer id, String userName, String mobile, String address) {
		// TODO Auto-generated method stub
		ResultSet rs = null;
	    String sql="update user set userName=?,mobile=?,address=? where id=?";
	  	List<Object> param = new ArrayList<Object>();
		param.add(userName);
		param.add(mobile);
		param.add(address);
		param.add(id);
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void changePassword(Integer id, String newPassword) {
		// TODO Auto-generated method stub
		ResultSet rs = null;
	    String sql="update user set password=? where id=? ";
	  	List<Object> param = new ArrayList<Object>();
		param.add(MD5Util.toMd5(newPassword));;
		param.add(id);
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}


}
